/**
 * Created by mj on 2017/3/30.
 */
$(function () {
    $('#dglist').datagrid({
        url: "ProcessControllerFindWithPaging",
        method: "post",
        title: "工艺流程卡列表",
        striped: true,
        singleSelect: true,
        rownumbers: true,
        loadMsg: "努力加载中...",
        fit: true,
        fitColumns: true,//是否自动填充适合屏幕
        pagination: true,//是否显示底部分页工具栏
        pageSize: 15,
        pageList: [15, 20, 25],
        showFooter: true,
        autoRowHeight: false,
        columns: [[
            {field: 'id', hidden: 'true'},
            {field: 'productOrder', title: '项目名称', sortable: true, width: 100, align: "center",
                formatter:function(value,row){
                    return row.productOrder.projectName;
                }},
            {field: 'partName', title: '零件名称', sortable: true, width: 100, align: "center"},
            {field: 'chartNumber', title: '图号', sortable: true, width: 100, align: "center"},
            {field: 'oderNumber', title: '订单数量', sortable: true, width: 100, align: "center"},
            {field: 'workOrderStatus', title: '状态', sortable: true, width: 100, align: "center"}
        ]],
        toolbar: [
            {
                text: '增加', iconCls: 'icon-add', handler: function () {
                $.messager.alert('消息', "应该由工艺师增加");
                // openAddTechnologyWin();
            }
            }, '-',
            {
                text: '修改', iconCls: 'icon-edit', handler: function () {
                $.messager.alert('消息', "应该由工艺师修改");
                // editProject();
            }
            }, '-',
            {
                text: '删除', iconCls: 'icon-remove', handler: function () {
                $.messager.alert('消息', "应该由工艺师删除");
                // deleteProject();
            }
            }, '-',
            {
                text: '查看', iconCls: 'icon-more', handler: function () {
                openDetailWin();
            }
            }, '-',
            {
                text: '刷新', iconCls: 'icon-reload', handler: function () {
                cancelWin('searchWin','searchForm');
                // $('#dglist').datagrid('reload',$.serializeObject());
                confirmSearch('dglist');
            }
            }, '-'
            ,
            {
                text: '打印', iconCls: 'icon-print', handler: function () {
                $.messager.alert('消息', "未确定是否由生产管理打印该流程单");
                // openWin("dglist", "importWin", "添加计划", 400, 400, "importForm", "save");
            }
            },
            '-'
            ,
            {
                text: '搜索', iconCls: 'icon-search', handler: function () {
                openSearchWin();
            }
            },
            '-'
            ,
            {
                text: '启动/暂停', iconCls: 'icon-lock', handler: function () {
                // $.messager.alert('消息', "未确定是否由生产管理打印该流程单");
                changedStatus();
            }
            },
            '-'
        ],
    });
    $('#dglist').datagrid('getPager').pagination({//分页栏下方文字显示
        beforePageText: '第',
        afterPageText: '页，总共 {pages}页',
        displayMsg: '当前显示从第{from}条到{to}条 共{total}条记录',
    });
});

//删除项目
function deleteProject() {
    var rows = $("#dglist").datagrid("getChecked");
    var ids = $.getManyRecords("dglist");
    var DNs = "";
    for (var i = 0; i < rows.length; i++) {
        DNs = DNs + rows[i].chartNumber + ",";
    }

    $.messager.confirm('确认', '您确认想要删除记录吗？', function (r) {
        $.ajax({
            type: "GET",
            url: "deleteProject",
            data: {ids: ids, DNs: DNs},
            timeout: 20000,
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data.msg == "success") {
                    $.messager.alert("提示", "删除成功！", "info", function () {
                        window.location.reload();
                    });
                } else {
                    $.messager.alert("提示", "删除失败！", "info", function () {
                        window.location.reload();
                    });
                }
            },
            error: function () {
                $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
            }
        })
    })
}

function openDetailWin() {
    if (!$.getSingleRecord("dglist")) {
        $('#detailWin').window('close');
        return;
    }
    $('#detailWin').window('open');
    var row = $.getSingleRecord("dglist");
    $('#detailList').datagrid({
        url: "findProcessFlowByWorkOrderId",
        method: "post",
        striped: true,
        loadMsg: "努力加载中...",
        queryParams: {workOrderId: row.id},
        fitColumns: true,
        autoRowHeight: false,
        columns: [[
            {field: 'sequenceNumber', title: '序号', sortable: true, align: "center"},
            {field: 'productionProcesses', title: '工序', sortable: true, align: "center"},
            {field: 'processContent', title: '工艺内容', sortable: true, align: "center"},
            {field: 'productionQuantity', title: '生产数量', sortable: true, align: "center"},
            {
                field: 'estimateTime', title: '预估工时', sortable: true, align: "center",
                formatter: function (value, row, index) {
                    return (row["estimateTime"]).toFixed(2);
                }
            },
            {field: 'operator', title: '操作员', sortable: true, width: 100, align: "center"},
            {field: 'receptionConfirmation', title: '下工段接收确认', sortable: true, width: 100, align: "center"},
            {field: 'remarks', title: '备注', sortable: true, width: 100, align: "center"}
        ]],
    });
}

function openSearchWin() {
    $("#searchWin").show().window("open");
}

function confirmSearch(listId) {
    $("#"+listId).datagrid('load',$.serializeObject());
    $("#searchWin").show().window("close");
}

function cancelSearch() {
    $("#searchWin").show().window("close");
}

function clearFrom() {
    $("#searchFrom").form('clear');
}

function changedStatus() {
    if (!$.getSingleRecord("dglist")) {
        $('#detailWin').window('close');
        return;
    }
    var id = $.getSingleRecord("dglist").id;
    var status = $.getSingleRecord("dglist").workOrderStatus;
    $.messager.confirm('确认', '您确认想要修改状态吗？', function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: "changeStatus",
                data: {id: id,status:status},
                timeout: 20000,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "修改成功！", "info", function () {
                            window.location.reload();
                        })
                    } else {
                        $.messager.alert("提示", "修改失败！", "info", function () {
                            window.location.reload();
                        });
                    }

                },
                error: function () {
                    $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
                }
            })
        }
    });
}